import { getAppChanges } from '../applications/app'
import { toBootstrapPromise } from '../lifecycles/bootstrap';
import { toLoadPromise } from '../lifecycles/load';
import { toMountPromise } from '../lifecycles/mount';
import { toUnmountPromise } from '../lifecycles/unmount';
import {startd} from '../start'
import { callCapturedEventListeners } from './route-event';
/** 加载应用，核心应用处理 */
export function reroute(){
  const { appsToLoad, appsToMount, appsToUnmount } = getAppChanges();
  if(startd){
    // app装载
    return preformAppchanges()
  }else{
    // 注册应用时，需要预先加载
    return loadApps()
  }
  /** 预加载应用 */
async function loadApps(){
  let apps = await Promise.all(appsToLoad.map(toLoadPromise))
  // callCapturedEventListeners(arguments)
}
/** 根据路径来装载应用 */
async function preformAppchanges(){
  // 1. 先卸载不需要的应用
  let unmountPromises = appsToUnmount.map(toUnmountPromise)
  // 2. 在加载需要的应用
  appsToLoad.map(async app => {
    // 先加载
    app = await toLoadPromise(app);
    app = await toBootstrapPromise(app)
    return toMountPromise(app)
  })
  // 3. 将需要挂载的应用进行挂载
  appsToMount.map(async app => {
    app = await toBootstrapPromise(app)
    return toMountPromise(app)
  })
}
}

